package com.longrise.co.ui.login.fragment;

import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.longrise.co.R;
import com.longrise.co.ui.login.contract.LoginContract;
import com.longrise.co.ui.login.model.ImageValidateCode;
import com.longrise.co.ui.login.model.LoginModel;
import com.longrise.co.ui.login.model.SmsCodeBean;
import com.longrise.co.ui.login.presenter.LoginPresenter;
import com.ly.common.base.BaseFragment;
import com.ly.common.commonutils.StringUtils;
import com.ly.common.commonutils.TimeCountUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by luoyang on 2017-6-22.
 */

public class ForgetPwdFragment extends BaseFragment<LoginPresenter, LoginModel> implements LoginContract.RegisterView {

    @Bind(R.id.pwd)
    EditText pwd;
    @Bind(R.id.confirm_pwd)
    EditText confirmPwd;
    @Bind(R.id.mobile)
    EditText mobile;
    @Bind(R.id.send_sms)
    TextView sendSms;
    @Bind(R.id.sms_code)
    EditText smsCode;
    @Bind(R.id.head_title)
    TextView headTitle;
    @Bind(R.id.eye)
    ImageView eye;
    @Bind(R.id.confirm_eye)
    ImageView confirm_eye;


    private boolean mIsShowPwd = false;
    private boolean mIsShowConfirmPwd = false;

    private TimeCountUtil mTimeCount;

    private String mCurrentSmsCode;

    @Override
    protected int getLayoutResource() {
        return R.layout.fg_forget_pwd;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    protected void initView() {
        setText(headTitle, "忘记密码");
        change2PwdDisplay(false);
    }


    public static ForgetPwdFragment newInstance() {
        return new ForgetPwdFragment();
    }

    @OnClick({R.id.send_sms,R.id.submit_register,R.id.eye, R.id.confirm_eye})
    public void onViewClicked(View view) {
        if (!checkDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.send_sms:
                if (mobile != null) {
                    mPresenter.sendSmsRequest(mobile.getText().toString(), "3");
                }

                break;
            case R.id.submit_register:
                if (StringUtils.isEmpty(mobile.getText().toString())) {
                    showErrorTip("请输入手机号");
                    return;
                }

                if (!StringUtils.isChinaPhoneLegal(mobile.getText().toString())) {
                    showErrorTip("请输入合法的手机号");
                    return;
                }

                if (StringUtils.isEmpty(smsCode.getText().toString())) {
                    showErrorTip("请输入验证码");
                    return;
                }

                if (StringUtils.isEmpty(pwd.getText().toString())) {
                    showErrorTip("请输入新密码");
                    return;
                }

                if (StringUtils.isEmpty(confirmPwd.getText().toString())) {
                    showErrorTip("请输入确认新密码");
                    return;
                }

                if(!pwd.getText().toString().equals(confirmPwd.getText().toString())) {
                    showErrorTip("确认新密码与新密码输入不一致");
                    return;
                }

                if(!smsCode.getText().toString().equals(mCurrentSmsCode)) {
                    showErrorTip("验证码输入有误");
                    return;
                }
                mPresenter.resetPwdRequest(mobile.getText().toString(), pwd.getText().toString());
                break;
            case R.id.eye:
                change2PwdDisplay(!mIsShowPwd);
                break;
            case R.id.confirm_eye:
                change2ConfirmPwdDisplay(!mIsShowConfirmPwd);
        }
    }

    private void change2PwdDisplay(boolean isShowPwd) {
        this.mIsShowPwd = isShowPwd;
        int resourceId = isShowPwd ? R.drawable.eye_orange : R.drawable.eye;
        eye.setBackgroundResource(resourceId);

        pwd.setInputType(isShowPwd ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD : InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    }

    private void change2ConfirmPwdDisplay(boolean isShowPwd) {
        this.mIsShowConfirmPwd = isShowPwd;
        int resourceId = isShowPwd ? R.drawable.eye_orange : R.drawable.eye;
        confirm_eye.setBackgroundResource(resourceId);

        confirmPwd.setInputType(isShowPwd ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD : InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    }


    @Override
    public void sendSmsSuccess(SmsCodeBean bean) {
        showLongToast("发送验证码成功");
        mCurrentSmsCode = bean.getSmscode();
        sendSms.setEnabled(false);
        if (mTimeCount == null) {
            mTimeCount = new TimeCountUtil(60 * 1000, 1000, sendSms);
        }
        mTimeCount.start();
    }

    @Override
    public void getImageValidateCodeSuccess(ImageValidateCode bean) {

    }

    @Override
    public void requestSuccess(Object o) {
        showLongToast("修改成功");
        _mActivity.onBackPressed();
    }


    @OnClick(R.id.head_back)
    public void onViewClicked() {
        _mActivity.onBackPressed();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mTimeCount != null) {
            mTimeCount = null;
        }
    }

}
